package com.cn.aam.checaiduo.ui.me;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListV2Listener;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.web.NormalWebActivity;
import com.cn.aam.checaiduo.bean.base.PageB;
import com.cn.aam.checaiduo.bean.me.PointB;
import com.cn.aam.checaiduo.bean.me.PointDetailB;
import com.cn.aam.checaiduo.bean.me.PointMallB;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.me.adapter.CalenderAdapter;
import com.cn.aam.checaiduo.ui.me.command.PointDetailCommand;
import com.cn.aam.checaiduo.utils.CalendarUtil;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.widget.WMToast;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * monker on 2017/5/25.
 */

@Route(path = UIRouter.ME.AC_SIGNIN)
public class SignInActivity extends BaseActivity {

    @BindView(R.id.sign_recyclerView)
    RecyclerView mRecycler;
    @BindView(R.id.sign_tv_point)
    TextView tvPoint;
    @BindView(R.id.sign_tv_signin)
    TextView tvSignIn;

    private boolean isSign;

    @Override
    protected int layoutId() {
        return R.layout.activity_sign_in;
    }

    @Override
    protected void initView() {
        super.initView();

        ToolBarManager.with(this, WMContent).setTitle("签到")
                .setNavigationIcon(R.drawable.ic_nav_arrow_white)
                .setBackground(R.drawable.bg_signin_filter)
                .setMenuTextContent("积分明细")
                .setMenuTextColor(R.color.white)
                .setMenuTextClick(new ToolBarManager.OnMenuTextClickListener() {
                    @Override
                    public void onMenuTextClick() {
                        if (MyViewsUtils.isFastDoubleClick()) return;
                        startActivity(PointDetailActivity.newInstance(mContext));
                    }
                });


        final List<CalenderAdapter.DateItemB> dateItemBList = CalendarUtil.getThisMonthDays();
        final CalenderAdapter adapter = new CalenderAdapter(mContext);
        adapter.setDataList(dateItemBList);


        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecycler.setLayoutManager(layoutManager);
        mRecycler.setAdapter(adapter);

        int today = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        if (today > 4 && today < (CalendarUtil.MaxDayOfMonth() - 3)) {
            layoutManager.scrollToPositionWithOffset(today - 4, 0);
            layoutManager.setStackFromEnd(true);
        } else if (today >= (CalendarUtil.MaxDayOfMonth() - 3)) {
            layoutManager.scrollToPositionWithOffset(CalendarUtil.MaxDayOfMonth() - 7, 0);
            layoutManager.setStackFromEnd(true);
        }
        getPoint();//获取积分
        String startTime = CalendarUtil.getCurMonth();
        PointDetailCommand command = new PointDetailCommand();
        command.setData("2017", "2", startTime);//当前月的签到记录
        command.execute("1", "31", new OnRequestListV2Listener<PointDetailB>(this) {
            @Override
            public void onNext(PageB<PointDetailB> value) {
                super.onNext(value);
                List<PointDetailB> list = value.getData();
                setSignInfo(list, dateItemBList);
                adapter.notifyDataSetChanged();

            }


        });
    }

    /**
     * 时间：2017/6/27
     * 描述：当前数hi件是否签到
     */
    private void setSignInfo(List<PointDetailB> list, List<CalenderAdapter.DateItemB> dateItemBList) {
        for (CalenderAdapter.DateItemB date : dateItemBList) {
            for (PointDetailB pointD : list) {
                String signDay = pointD.getCreated_time().split(" ")[0];
                String dateStr = date.getDetailDate();
                if (TextUtils.equals(signDay, dateStr)) {
                    date.setSign(true);
                    if (date.isToday()) {
                        tvSignIn.setText("已签到");
                        isSign = true;
                        tvSignIn.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_circle_has_signin));
                    }
                    break;
                }
            }
        }
    }

    //签到
    @OnClick(R.id.sign_tv_signin)
    void clickToSignIn() {
        if (MyViewsUtils.isFastDoubleClick()) return;

        if (!isSign)
            signIn();
    }

    //积分商城
    @OnClick(R.id.sign_tv_pointMall)
    void clickToPointMall() {
        if (MyViewsUtils.isFastDoubleClick()) return;
        gotoPointMall();
    }

    //积分攻略
    @OnClick(R.id.sign_tv_introduce)
    void clickToIntroduce() {
        if (MyViewsUtils.isFastDoubleClick()) return;
        startActivity(PointIntroduceActivity.newInstance(mContext));
    }


    /**
     * 时间：2017/6/21
     * 描述：获取积分
     */
    private void getPoint() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .getPoint(AppData.getToken())
                .compose(RxSchedulers.<PointB>toMain())
                .subscribe(new OnRequestListener<PointB>(this) {
                    @Override
                    public void onNext(PointB value) {
                        super.onNext(value);
                        tvPoint.setText(value.getPoint() + "分");
                    }
                });
    }

    /**
     * 时间：2017/6/27
     * 描述：签到
     */
    private void signIn() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .signin(AppData.getToken())
                .compose(RxSchedulers.<PointB>toMain())
                .subscribe(new OnRequestListener<PointB>(this) {
                    @Override
                    protected boolean isShowProgressBar() {
                        return false;
                    }

                    @Override
                    public void onNext(PointB value) {
                        super.onNext(value);
                        if (value.getCode() == 1) {
                            tvPoint.setText(value.getPoint() + "分");
                            WMToast.normal(value.getMsg());
                            tvSignIn.setText("已签到");
                            isSign = true;
                            tvSignIn.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_circle_has_signin));
                        }
                    }
                });
    }


    /**
     * 时间：2017/6/15
     * 描述：商城
     */
    private void gotoPointMall() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .pointMall(AppData.getToken())
                .compose(RxSchedulers.<PointMallB>toMain())
                .subscribe(new OnRequestListener<PointMallB>(this) {
                    @Override
                    protected boolean isShowProgressBar() {
                        return false;
                    }

                    @Override
                    public void onNext(PointMallB value) {
                        super.onNext(value);
                        if (value != null)
                            startActivity(NormalWebActivity.newInstance(mContext, value.getUrl(), "我的积分"));
                    }
                });
    }
}
